﻿Imports Microsoft.Phone.Info
Imports LSW.WP8.Utility
Imports System.Collections

Namespace Exceptions
    Public Class LSWFrameworkException
        Inherits Exception

        Private BaseEx As Exception
        Private DeviceUniqueId As String

        Public Sub New(message As String)
            MyClass.New(New Exception(message))
        End Sub

        Public Sub New(ex As Exception)
            BaseEx = ex
            DeviceUniqueId = SysHelper.GetDeviceUniqueId
        End Sub

        Public Overrides Function GetBaseException() As Exception
            Return BaseEx
        End Function

        Public Overrides ReadOnly Property Message As String
            Get
                Return BaseEx.Message
            End Get
        End Property

        Public Overrides Function ToString() As String
            Dim xml = <Report DeviceUniqueId=<%= DeviceUniqueId %> DataTime=<%= Date.Now %>>
                          <Datas>
                              <Data Key=<%= From entry In BaseEx.Data.Cast(Of DictionaryEntry)() Select entry.Key %>>
                                  <%= From entry In BaseEx.Data.Cast(Of DictionaryEntry)() Select entry.Value %>
                              </Data>
                          </Datas>
                          <ExceptionType><%= BaseEx.GetType.FullName %></ExceptionType>
                          <Message><%= BaseEx.Message %></Message>
                          <Source><%= BaseEx.Source %></Source>
                          <StackTrace><%= BaseEx.StackTrace %></StackTrace>
                      </Report>
            Return xml.ToString
        End Function

        Public Async Sub SendToUrl()
            Const FeedBackUrl = "http://softwave.lishewen.com/api/feedback"
            Dim datas As New Dictionary(Of String, String)
            datas.Add("a", "Exception")
            datas.Add("t", Me.ToString)
            Dim result = Await WebHelper.Post(FeedBackUrl, datas)
            If String.IsNullOrWhiteSpace(result) Then
                Throw New ArgumentException("网址不存在 或 提交失败")
            End If
        End Sub
    End Class
End Namespace